'''
Author: nihao642250185@163.com nihao642250185@163.com
Date: 2023-08-14 16:02:13
LastEditors: nihao642250185@163.com nihao642250185@163.com
LastEditTime: 2023-08-14 16:25:21
FilePath: \peri\peri.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''

import random
import logging
import requests
from lxml import etree
from com import Common
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(name)s:%(levelname)s: %(message)s')


class Peri(object):

    def __init__(self):
        self.common = Common()

        self.SECRET = "SECf3b6f126204991372cbafb6bf8939f83d3c61e87f94b230371f28c5524ce98b1"
        self.ACCESS_TOKEN = "0fbb81d0d66807df6c380f3a42dd738e173bdbfffd87991d34b7bea9eab34b54"

        pass

    def __home_page(self, pages):

        url = "http://m.jj20.com/bz/nxxz/nxmt/list_57_{}.html".format(pages)    # 请求的链接地址

        headers = {
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
        }

        girl = None
        try:
            res = requests.get(url, headers=headers)    # 发送请求
            res.encoding = "gbk"    # 设置请求编码格式为GBK
            if res.status_code == 200:  # 请求成功
                html = etree.HTML(res.text) # 通过etree.HTML方法解析成html
                lis = html.xpath("//ul[@class='ul_half']/li")   # 获取网页中标签。
                logging.info("lis.size: %s" % len(lis)) # 打印
                girls = []
                for item in lis:    # 循环遍历采集需要的值
                    subpage = item.xpath("./div[@class='pic_box']/a/@href")
                    images = item.xpath("./div[@class='pic_box']/a/img/@src")
                    introduction = item.xpath("./div[@class='pic_box']/a/img/@alt")
                    # 将对象添加到列表
                    girls.append({
                        "subpage": subpage[0],
                        "images": images[0],
                        "title": introduction[0]
                    })
                girl = random.choice(girls) # 随机取一个
                logging.info("girl: %s" % girl) # 打印
            else:
                logging.warning("the request failed!")  # 告警打印
        except Exception as e:
            logging.error("Request exception %s" % e)   # 错误打印
        finally:
            logging.info("done")
            return girl # 返回女孩
        pass

    def girl_info(self):
        pages = random.randint(1, 101)  # 随机访问一页
        girl = self.__home_page(pages)  # 调用__home_page方法
        try:
            res = requests.get(girl['subpage'])
            res.encoding = "gbk"
            if res.status_code == 200:
                html = etree.HTML(res.text)
                images = html.xpath("//div[@class='info_pic']/a/img/@src")
                logging.info("images: %s" % images[0])
                text = "Appreciate the vast."
                self.common.dingtalk_robot(self.SECRET, self.ACCESS_TOKEN, text, girl['title'], girl['images'], images[0])
            else:
                logging.warning("Sorry, no data!")
        except Exception as e:
            logging.error("Request exception: %s" % e)
        finally:
            pass
        pass
    pass

if __name__ == "__main__":

    peri = Peri()       # 实例化类
    peri.girl_info()    # 调用女孩信息方法
    print("nihao")
    pass

